package com.xiaoyg;

import java.io.*;
import java.net.*;

/**
 * @author xiaoyg
 * @Title: Client
 * @ProjectName socketLearn
 * @Description: TODO
 * @date 2019/4/1614:51
 */
public class Client {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket();

        socket.setSoTimeout(3000);
        socket.connect(new InetSocketAddress(Inet4Address.getLocalHost(),2000),3000);

        System.out.println("已发起服务器连接，进入后续流程~");
        System.out.println("客户端信息："+socket.getLocalAddress()+" P:"+socket.getLocalPort());
        System.out.println("客户端信息："+socket.getInetAddress()+" P:"+socket.getPort());
        System.out.println("已发起服务器连接，进入后续流程~");

        try{
            //发送接收数据
            todo(socket);
        }catch (Exception e){
            System.out.println("异常关闭");
        }

        //释放资源
        socket.close();
        System.out.println("客户端已退出~");
    }

    private static void todo(Socket client) throws IOException{
        //构建键盘输入流
        InputStream in = System.in;


        BufferedReader input = new BufferedReader(new InputStreamReader(in));

        InputStream inputStream = client.getInputStream();


        OutputStream outputStream = client.getOutputStream();

        PrintStream socketPrintStream = new PrintStream(outputStream);

        String string = input.readLine();

        socketPrintStream.println(string);


        String echo = input.readLine();
        if ("bye".equalsIgnoreCase(echo)){

        }

    }


}
